package com.spring.service.impl;

import com.spring.dao.AccountDao;
import com.spring.domain.Account;
import com.spring.service.AccountService;
import com.spring.util.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//@Service("TcfService")
public class AccountServiceTCF implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private TransactionManager transactionManager;

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Override
    public Account findById(int id) {
        return accountDao.findById(id);
    }

    @Override
    public void delById(int id) {
        accountDao.delById(id);
    }

    @Override
    public void transfer(String send, String receive, double money) {
        try {
            //开启事务
            transactionManager.begin();

            //业务代码
            accountDao.update(send,0-money);
            int i = 1/0;
            accountDao.update(receive, money);

            //提交事务
            transactionManager.commit();
        }catch (Exception e){
            //回滚事务
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            transactionManager.close();
        }
    }
}
